﻿#include "Student.h"
// 默认构造
Student::Student()
{
}
Student::Student(int id, string name, string pwd)
{
	this->m_Id = id;
	this->m_Name = name;
	this->m_Pwd = pwd;
}
// 有参构造
Student::Student(int id, string name, string pwd, OrderFile* o, ComputerRoomFile* c)
{
	this->m_Id = id;
	this->m_Name = name;
	this->m_Pwd = pwd;
	this->m_orderFile = o;
	this->m_ComputerRoomFile = c;
	this->m_mMyOrder.clear();
	this->m_orderFile->getOrderById(this->m_Id, this->m_mMyOrder);
}

// 默认析构
Student::~Student()
{
}

// 学生菜单界面
void Student::operMenu()
{
	cout << "欢迎学生代表：" << this->m_Name << "登录！" << endl;
	cout << "\t\t ----------------------------------\n";
	cout << "\t\t|                                 |\n";
	cout << "\t\t|          1.申请预约             |\n";
	cout << "\t\t|                                 |\n";
	cout << "\t\t|          2.查看我的预约         |\n";
	cout << "\t\t|                                 |\n";
	cout << "\t\t|          3.查看所有预约         |\n";
	cout << "\t\t|                                 |\n";
	cout << "\t\t|          4.取消预约             |\n";
	cout << "\t\t|                                 |\n";
	cout << "\t\t|          0.注销登录             |\n";
	cout << "\t\t|                                 |\n";
	cout << "\t\t ----------------------------------\n";
	cout << "请选择您的操作： " << endl;
}

// 申请预约
void Student::applyOrder()
{
	int date;
	int interval;
	int roomId;

	cout << "机房开放时间为周一至周五！" << endl;
	cout << "请输入申请预约的时间：" << endl;
	cout << "1、周一" << endl;
	cout << "2、周二" << endl;
	cout << "3、周三" << endl;
	cout << "4、周四" << endl;
	cout << "5、周五" << endl;
	while (true)
	{
		cin >> date;
		if (date >= 1 && date <= 5)
		{
			break;
		}
		cout << "输入有误，请重新输入：" << endl;
	}

	cout << "请输入申请预约的时间段：" << endl;
	cout << "1、上午" << endl;
	cout << "2、下午" << endl;
	while (true)
	{
		cin >> interval;
		if (interval == 1 || interval == 2)
		{
			break;
		}
		cout << "输入有误，请重新输入：" << endl;
	}

	cout << "请选择机房：" << endl;
	//ComputerRoom::showInfo();
	this->m_ComputerRoomFile->showInfo();
	while (true)
	{
		cin >> roomId;
		if (roomId >= 1 && roomId <= 3)
		{
			break;
		}
		cout << "输入有误，请重新输入：" << endl;
	}

	Order order(this->m_Id, this->m_Name, date, interval, roomId, ORDERING);
	this->m_orderFile->addOrder(order);

	this->m_ComputerRoomFile->updateCurrentNumById(roomId, -1);
	cout << "预约成功！" << endl;

	// 数据更新;
	this->m_mMyOrder.clear();
	this->m_orderFile->getOrderById(this->m_Id, this->m_mMyOrder);

	system("pause");
	system("cls");
}

// 展示我的预约
void Student::showMyOrder()
{
	if (this->m_mMyOrder.size() == 0)
	{
		cout << "无预约信息！" << endl;
		system("pause");
		system("cls");
		return;
	}
	int index = 1;
	for (map<int, Order>::iterator it = this->m_mMyOrder.begin(); it != this->m_mMyOrder.end(); it++)
	{
		cout << "第" << index << "条："
			<< "时间：" << DATE[it->second.m_Date]
			<< INTERVAL[it->second.m_Interval] << " "
			<< "地点：" << it->second.m_RoomId << "号机房 "
			<< "状态：" << STATUS[it->second.m_Status] << endl;
		index++;
	}
	system("pause");
	system("cls");
}

// 展示所有预约
void Student::showAllOrder()
{
	cout << "--------------所有预约信息如下：--------------" << endl;
	this->m_orderFile->showAllOrder();
	system("pause");
	system("cls");
}

// 取消预约
void Student::cancelOrder()
{
	if (this->m_mMyOrder.size() == 0)
	{
		cout << "无预约信息！" << endl;
		system("pause");
		system("cls");
		return;
	}
	cout << "审核中或预约成功的记录可以取消，这是你可以取消的预约记录：" << endl;
	//int num = 0;// 记录有几条可以被取消的记录
	vector<int> vIndex;// 记录可以取消的记录的索引号
	for (map<int, Order>::iterator it = this->m_mMyOrder.begin(); it != this->m_mMyOrder.end(); it++)
	{
		if (it->second.m_Status == ORDERING || it->second.m_Status == ORDERED)
		{
			cout << "第" << vIndex.size() + 1 << "条："
				<< "时间：" << DATE[it->second.m_Date]
				<< INTERVAL[it->second.m_Interval] << " "
				<< "地点：" << it->second.m_RoomId << "号机房 "
				<< "状态：" << STATUS[it->second.m_Status] << endl;
			vIndex.push_back(it->first);
			//num++;
		}
	}
	cout << "请输入需要取消的预约记录(0-返回)：" << endl;
	int select;
	while (true)
	{
		cin >> select;
		if (select == 0)
		{
			break;
		}
		else if (select > 0 && select <= (int)vIndex.size())
		{
			this->m_orderFile->cancelOrder(vIndex[select - 1]);
			//cout << "您取消了" << this->m_mMyOrder[vIndex[select - 1]].m_RoomId << "机房的预约！" << endl;
			this->m_ComputerRoomFile
				->updateCurrentNumById(this->m_mMyOrder[vIndex[select - 1]].m_RoomId, 1);
			// 数据更新
			this->m_mMyOrder.clear();
			this->m_orderFile->getOrderById(this->m_Id, this->m_mMyOrder);

			cout << "取消成功！" << endl;
			break;
		}
		cout << "输入有误，请重新输入：" << endl;
	}
	system("pause");
	system("cls");
}